package fun.ui;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

import org.lex.swing.widgets.MemoryPane;

import fun.configuration.CK;
import fun.configuration.Config;
import fun.ui.frames.ConsoleFrame;
import fun.ui.frames.LogFrame;
import fun.ui.frames.ResultFrame;
import fun.ui.frames.SymbolTableFrame;

public class FunMainFrame extends JFrame {
	private JMenuBar mainMenuBar = null;
	private JDesktopPane desktopPane = null;
	private JPanel pane = null;
	private JPanel statusPane = null;
	private JLabel statusLabel = null;
	// ================================
	// private CommandFrame commandFrame = null;
	private ConsoleFrame consoleFrame = null;
	private LogFrame logFrame = null;
	private ResultFrame resultFrame = null;
	private SymbolTableFrame symbolTableFrame = null;

	public FunMainFrame() {
		super();
		Splash.setStatus("Loading Symbol Table...");
		this.initialize();
		Splash.setStatus("Initialize Components...");
		this.initializeInternalFrames();
		// this.getDesktopPane().getDesktopManager().activateFrame(
		// getConsoleFrame() );
		this.getDesktopPane().setSelectedFrame(this.getConsoleFrame());
	}

	public ConsoleFrame getConsoleFrame() {
		if (null == consoleFrame) {
			consoleFrame = new ConsoleFrame(this);
			consoleFrame.setVisible(true);
		}
		return consoleFrame;
	}

	// public CommandFrame getCommandFrame()
	// {
	// if( null == this.commandFrame )
	// {
	// this.commandFrame = new CommandFrame( this );
	// this.commandFrame.setVisible( true );
	// }
	// return this.commandFrame;
	// }
	public LogFrame getLogFrame() {
		if (null == this.logFrame) {
			this.logFrame = new LogFrame(this);
			this.logFrame.setVisible(true);
		}
		return this.logFrame;
	}

	public ResultFrame getResultFrame() {
		if (null == this.resultFrame) {
			this.resultFrame = new ResultFrame(this);
			this.resultFrame.setVisible(true);
		}
		return this.resultFrame;
	}

	public SymbolTableFrame getSymbolTableFrame() {
		if (null == this.symbolTableFrame) {
			this.symbolTableFrame = new SymbolTableFrame(this);
			this.symbolTableFrame.setVisible(true);
		}
		return this.symbolTableFrame;
	}

	public void addInternalFrame(JInternalFrame frame) {
		this.getDesktopPane().add(frame);
		frame.setVisible(true);
	}

	private void initializeInternalFrames() {
		this.getDesktopPane().add(this.getLogFrame());
		this.getDesktopPane().add(this.getConsoleFrame());
		this.getDesktopPane().add(this.getResultFrame());
		this.getDesktopPane().add(this.getSymbolTableFrame());
		this.loadConfigurations();
	}

	private void loadConfigurations() {
		this.setBounds(Config.getRectangle(CK.UI_MAIN_WINDOW_BOUNDS));
		consoleFrame.setBounds(Config.getRectangle(CK.UI_COMMAND_WINDOW_BOUNDS));
		logFrame.setBounds(Config.getRectangle(CK.UI_LOG_WINDOW_BOUNDS));
		resultFrame.setBounds(Config.getRectangle(CK.UI_RESULT_WINDOW_BOUNDS));
		symbolTableFrame.setBounds(Config.getRectangle(CK.UI_SYMBOL_TABLE_WINDOW_BOUNDS));
	}

	void exit() {
		// save configurations
		Config.setRectangle(CK.UI_MAIN_WINDOW_BOUNDS, this.getBounds());
		Config.setRectangle(CK.UI_COMMAND_WINDOW_BOUNDS, consoleFrame.getBounds());
		Config.setRectangle(CK.UI_LOG_WINDOW_BOUNDS, logFrame.getBounds());
		Config.setRectangle(CK.UI_RESULT_WINDOW_BOUNDS, resultFrame.getBounds());
		Config.setRectangle(CK.UI_SYMBOL_TABLE_WINDOW_BOUNDS, symbolTableFrame.getBounds());
		Config.storeConfigurationToFile();
		// then exit
		this.dispose();
		// System.exit( 0 );
	}

	// =======================
	private void initialize() {
	this.setSize(600, 500);
	this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/resources/icon.png")));
	this.setJMenuBar(getMainMenuBar());
	this.setContentPane(getPane());
	this.setTitle("Fun");
	this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	this.addWindowListener(new WindowAdapter() {
	    @Override
	    public void windowClosing(WindowEvent e) {
		exit();
	    }
	});
    }

	private JMenuBar getMainMenuBar() {
		if (mainMenuBar == null) {
			mainMenuBar = new MainMenuBar(this);
		}
		return mainMenuBar;
	}

	private JDesktopPane getDesktopPane() {
		if (desktopPane == null) {
			desktopPane = new JDesktopPane();
			desktopPane.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
		}
		return desktopPane;
	}

	private JPanel getPane() {
		if (pane == null) {
			pane = new JPanel();
			pane.setLayout(new BorderLayout());
			pane.add(getDesktopPane(), BorderLayout.CENTER);
			pane.add(getStatusPane(), BorderLayout.SOUTH);
		}
		return pane;
	}

	private JPanel getStatusPane() {
		if (statusPane == null) {
			statusLabel = new JLabel();
			statusLabel.setText("StatusLabel OK.");
			statusPane = new JPanel();
			statusPane.setBorder(BorderFactory.createEtchedBorder());
			statusPane.setLayout(new BorderLayout());
			statusPane.add(statusLabel, BorderLayout.CENTER);
			statusPane.add(new MemoryPane(), BorderLayout.EAST);
		}
		return statusPane;
	}
}
